#ifndef TAN_OR_H
#define TAN_OR_H

#include "stdio.h"
#include "stdlib.h"
#include <list>


#include "sharedStructure.hpp"
#include "projector.h"
#include "imgData.h"
#include "activeCurve.h"

class tanOR{
public:
	tanOR():n_orient(18),n_length(8),h(8),max_rotation(1),n_color_bin(16){};
	~tanOR(){};

	bool initialTanORNode(const mxArray *p_res_tree,float R[], float T[], float swt,const char* tan_path,int i_idx, int i_layer, int i_node, FILE *p_weight=NULL);
	void proposeParticle(const lineVertex &line1, const lineVertex &line2, tanParticle_s &ptc1, tanParticle_s &ptc2);
	bool inference(line &mat_line_1,line &mat_line_2);
	bool inference(lineVertex &line1, lineVertex &line2);
	bool test(float P[][4],float cam_dir[],float &score, int &arg_max);
	bool argMax2(float P[][4],float cam_dir[3], int arg_max, tanParticle_s &tan_prop, float *p_template=NULL);

	bool release();

	void updateImageDataRef(imgData *p);

	std::list<tanParticle_s> tan_particle_list;
	activeShape tan_shape;
	projector camera;
	void readColorModel(const char* path_name);
	void initialColorGrid();
	void initialFlatGrid();
	float testColor(int i_deform, float * p_color_feature_veci=NULL);
	float testFlat(int i_deform, float * p_color_feature_veci=NULL);

	void drawColor(int arg_max, float* p_template);
	void drawFlat(int arg_max, float* p_template);
	float *SWTest(view_s &view,bool use_color);

	int getID(){return this->id;};

	float complexity_weight;
private:

	// private variables
	/*
	struct s_color_model{
	float x,y;
	float mu1,mu2,mu3;
	float sigma1,sigma2,sigma3;

	std::vector<double> logP;
	};

	std::deque<s_color_model> color_model;
	*/


	int n_color_bin;
	std::deque<std::vector<float> > color_deform_grid_x,color_deform_grid_y;
	std::deque<std::vector<float> > flat_deform_grid_x,flat_deform_grid_y;
	std::vector<float> color_gridX,color_gridY; 
	std::vector<float> flat_gridX,flat_gridY;
	float *p_sk_weight;
	float clr_weight[30];
	float flat_weight[10];

	int n_orient,n_angle,n_length;
	int h; // filter size

	plane current_plane;
	activeCurve irac;
	int max_rotation ;
	float **R_array;
	void pushDeformations();
	void pushColorDeformations();
	void pushFlatDeformations();
	int id;

	double pointAngle(float x, float y);
	void matchLine(const lineVertex &img_l1, const lineVertex &img_l2, std::deque<pt_pair_s> &pair_1, std::deque<pt_pair_s> &pair_2);
	int sub2IndS2(int i_img, int i_orient, int i_angle, int i_length);

	bool updateParticleList(tanParticle_s & particle);

	imgData *p_img_data;

	float shiftmax(const line &matlab_line,line &max_line);
};



#endif
